/* Copyright 2020 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: sxc
 * Date  : 2023.12.6
 *
 */
 #include <stdio.h>
#include "unistd.h"     //sleep
#include "stdlib.h"
#include "hdf_base.h"
#include "hdf_io_service_if.h"

#include "hilog/log.h"  // use HILOG_INFO to print log should include this .h

#undef  LOG_TAG
#undef  LOG_DOMAIN
#define LOG_TAG    "led_rgb"
#define LOG_DOMAIN 0xD002022
#define LED_SERVICE_NAME "led_service"

#define LED_RGB_WRITE         7
#define LED_R_BIT 0x01
#define LED_G_BIT 0x02
#define LED_B_BIT 0x04

int main(int argc, char* argv[])
{
    printf("start\n");
    int ret = HDF_SUCCESS;
	int32_t led_mod = -1;
	if (argc >= 2) {
        led_mod = atoi(argv[1]);
        led_mod &= 0x07;        //need only lo-3bits

        HILOG_INFO(LOG_APP, "[%{public}s] main enter: led_mod[%{public}s%{public}s%{public}s][0x%{public}X]",
                LOG_TAG, (led_mod&LED_B_BIT)?"B":"-", (led_mod&LED_G_BIT)?"G":"-",
                (led_mod&LED_R_BIT)?"R":"-", led_mod);
	}
    else{
        HILOG_INFO(LOG_APP, "[%s] main enter: Auto Test", LOG_TAG);
        printf("[%s] main enter: Auto Test\n", LOG_TAG);
    }
        
        
    // 用户态获取驱动的服务，获取该服务之后通过服务中的Dispatch方法向驱动发送消息。
    struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE_NAME);
    if (serv == NULL) {
        HILOG_ERROR(LOG_APP, "get service %{public}s NG", LED_SERVICE_NAME);
        printf("get service %s NG\n", LED_SERVICE_NAME);
        return -1;
    }
    //Shared Buffer 是一种在进程间共享数据的机制，通过共享内存来传递数据
    struct HdfSBuf *data = HdfSbufObtainDefaultSize();  //default 256Bytes data
    if (data == NULL) {
        HILOG_ERROR(LOG_APP, "obtain data NG");
        return -1;
    }

    if(led_mod == -1) {
        led_mod = 0x08;
        while (led_mod) {
            HdfSbufFlush(data);
            // 向 Hdf SBuf（Shared Buffer）data中写入一个 32 位整数
            if (!HdfSbufWriteInt32(data, --led_mod)) {
                HILOG_ERROR(LOG_APP, "write data NG");
                return -1;
            }
            // 通过服务中的Dispatch方法向驱动发送消息。
            ret = serv->dispatcher->Dispatch(&serv->object, LED_RGB_WRITE, data, NULL);
            sleep(2);
        }
    }
    else {
        if (!HdfSbufWriteInt32(data, led_mod)) {
            HILOG_ERROR(LOG_APP, "write data NG");
            return -1;
        }
        ret = serv->dispatcher->Dispatch(&serv->object, LED_RGB_WRITE, data, NULL);
    }
    // 回收 Hdf SBuf（Shared Buffer） 的内存资源
    HdfSbufRecycle(data);
    // 释放驱动服务
    HdfIoServiceRecycle(serv);
	HILOG_INFO(LOG_APP, "[%{public}s] main exit.", LOG_TAG);

    return ret;
}
